//
//  BIDExpressCompanyListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/10.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDExpressCompanyListViewController.h"
#import "BIDDatabaseManager.h"

@interface BIDExpressCompanyListViewController ()
{
    NSArray *_dataSourceArr;
}
@end

@implementation BIDExpressCompanyListViewController
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"快递公司";
    BIDDatabaseManager *db = [BIDDatabaseManager getInstance];
    [db openDB];
    NSArray *arr = [db queryFromTable:@"express100" fields:@[@"name", @"code"] condition:@{} index:-1];
    _dataSourceArr = [[NSArray alloc] initWithArray:arr];
    [db closeDB];
    _tableView.tableFooterView = [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dic = _dataSourceArr[row];
    cell.textLabel.text = dic[@"name"];    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _dataSourceArr[indexPath.row];
    [delegate fetchExpressCompanyName:dic[@"name"] code:dic[@"code"]];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
